package com.hsnn.medstgmini.trading.catalog.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hsnn.medstgmini.pro.base.model.ViewGoods;
import com.hsnn.medstgmini.trading.catalog.dao.HospCatalogDao;
import com.hsnn.medstgmini.trading.catalog.model.SubGoods;
import com.hsnn.medstgmini.trading.catalog.model.SubHospInfo;
import com.hsnn.medstgmini.trading.catalog.model.form.GoodsSelectForm;
import com.hsnn.medstgmini.trading.catalog.service.HospCatalogManager;
import com.hsnn.medstgmini.util.Pagination;

/**
 * @category 医院商品目录查看
 * @author 蔡春龙
 * @date 2015年6月14日
 */
@Service
public class HospCatalogManagerImpl implements HospCatalogManager {
	@Resource(name = "sessionFactory")
	private SessionFactory sessionFactory;
	@Autowired
	private HospCatalogDao hospCatalogDao;

	/**
	 * @category 查询医疗机构列表
	 * @date 2015年7月7日
	 * @param hospInfo
	 * @return
	 */
	public Pagination getHospList(SubHospInfo hospInfo, Pagination page) {
		return hospCatalogDao.getHospList(hospInfo, page);
	}

	/**
	 * @category 根据医疗机构查询商品列表
	 * @date 2015年7月7日
	 * @param goods
	 * @param page
	 * @return
	 */
	public Pagination getCatalogByHosp(SubGoods goods, Pagination page) {
		return hospCatalogDao.getCatalogByHosp(goods, page);
	}

	/**
	 * @category 查询商品列表
	 * @date 2015年7月7日
	 * @param goods
	 * @return
	 */
	public Pagination getGoodsList(SubGoods goods, Pagination page) {
		return hospCatalogDao.getGoodsList(goods, page);
	}

	/**
	 * @category 查询商品下的医疗机构采购目录列表(中心根据商品查看旗下的医疗机构)
	 * @date 2015年7月7日
	 * @param hospInfo
	 * @param page
	 * @return
	 */
	public Pagination getCatalogByGoods(SubHospInfo hospInfo, Pagination page) {
		return hospCatalogDao.getCatalogByGoods(hospInfo, page);
	}

	/**
	 * @category 查询goods信息
	 * @date 2015年7月8日
	 * @param goodsCode
	 * @return
	 */
	public ViewGoods getViewGoodsByCode(String goodsCode) {
		return hospCatalogDao.getViewGoodsByCode(goodsCode);
	}

	@Override
	public String getAreaIdByOrgId(String orgId) {
		return hospCatalogDao.getAreaIdByOrgId(orgId);
	}

	@Override
	public Pagination getHospListBySC(SubHospInfo hospInfo, Pagination page) {
		return hospCatalogDao.getHospListBySC(hospInfo,page);
	}


	@Override
	public Pagination getCatalogByHospSC(SubGoods goods, Pagination page) {
		return hospCatalogDao.getCatalogByHospSC(goods,page);
	}
	public void batchDelete(List<GoodsSelectForm> list,String hospCode){
		if (null != list && list.size() > 0){
			List<String> pl = new ArrayList<String>();
			for (int i = 0; i < list.size(); i++){
				GoodsSelectForm form = list.get(i);
				pl.add(form.getGoodsCode());
			}
			try {
				sessionFactory.getCurrentSession().getTransaction().begin();
				hospCatalogDao.batchDelete(pl,hospCode);
				sessionFactory.getCurrentSession().getTransaction().commit();
			} catch (Exception e) {
				sessionFactory.getCurrentSession().getTransaction().rollback();
			}
			
		}
	}

}
